package com.ytjj.qmyx.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.ytjj.common.api.CommonPage;
import com.ytjj.common.api.CommonResult;
import com.ytjj.qmyx.admin.mapper.HomeImageMapper;
import com.ytjj.qmyx.admin.model.HomeImage;
import com.ytjj.qmyx.admin.model.HomeImageExample;
import com.ytjj.qmyx.admin.model.request.HomeImageRequest;
import com.ytjj.qmyx.admin.model.response.HomeImageResponse;
import com.ytjj.qmyx.admin.service.HomeImageService;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class HomeImageServiceImpl implements HomeImageService {

    @Resource
    private HomeImageMapper homeImageMapper;


    @Override
    public CommonResult addOrUpdate(HomeImage homeImage) {
        if (null == homeImage.getId()){
            homeImage.setCreateTime(new Date());
            return CommonResult.success(homeImageMapper.insertSelective(homeImage));
        }else {
            return CommonResult.success(homeImageMapper.updateByPrimaryKeySelective(homeImage));
        }

    }

    @Override
    public CommonPage list(HomeImageRequest request) {
        HomeImageExample homeImageExample = new HomeImageExample();
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        return CommonPage.restPage(homeImageMapper.selectByExample(homeImageExample));
    }

    @Override
    public CommonResult delete(Integer id) {
        return CommonResult.success(homeImageMapper.deleteByPrimaryKey(id));
    }

    @Override
    public HomeImageResponse getHomeImage(Integer id) {
        HomeImage homeImage = homeImageMapper.selectByPrimaryKey(id);
        HomeImageResponse homeImageResponse = new HomeImageResponse();
        BeanUtils.copyProperties(homeImage,homeImageResponse);
        return homeImageResponse;
    }
}
